package starComponent;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import starComponent.elements.Ladder;
import starComponent.elements.StarBranchSlider;
import starComponent.elements.StarComponent;
import tuio.components.ButtonTouch;
import tuio.components.TuioFrame;
import tuio.libTUIO.src.TUIO.TuioClient;

/**
 * @author Charles Perin
 * 21 avr. 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class StarComparatorTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {


		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				final TuioFrame frame = new TuioFrame("StarComparatorTest");
				TuioClient client = new TuioClient();
				client.addTuioListener(frame);
				client.connect();

				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


				frame.setPreferredSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize()));
				frame.setUndecorated(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				
				final StarComponent star1 = new StarComponent(20,250, Color.RED);
				star1.setName("Star1");
				star1.setBounds(0, 0, (int)(frame.getPreferredSize().getWidth()/2-50), (int)(frame.getPreferredSize().getHeight()/2-5));
				star1.setBorder(BorderFactory.createTitledBorder("Star 1"));
				final StarComponent star2 = new StarComponent(20,250, Color.BLUE);
				star2.setName("Star2");
				star2.setBounds((int)(frame.getPreferredSize().getWidth()/2-50), 0,  (int)(frame.getPreferredSize().getWidth()/2-50), (int) (frame.getPreferredSize().getHeight()/2-5));
				star2.setBorder(BorderFactory.createTitledBorder("Star 2"));
				star1.getControler().init();
				star1.setBranchThickness(3);
				star1.getControler().setLinkBranches(true);
				star1.setLinkThickness(3);
				star1.setCentralPolygonColor(Color.BLACK);
				star1.setCentralPolygonThickness(1);
				star1.getControler().setCollaborative(false);
				star1.getControler().setPastTrace(false);
				star1.getControler().setPastTraceTime(5000);
				star1.getControler().setPastTraceFrequency(50);
				star2.getControler().init();
				star2.setBranchThickness(3);
				star2.getControler().setLinkBranches(true);
				star2.setLinkThickness(3);
				star2.setCentralPolygonColor(Color.BLACK);
				star2.setCentralPolygonThickness(1);
				star2.getControler().setCollaborative(false);
				star2.getControler().setPastTrace(false);
				star2.getControler().setPastTraceTime(5000);
				star2.getControler().setPastTraceFrequency(50);
				
				ButtonTouch exitButton = new ButtonTouch("Exit");
				exitButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						System.exit(0);
					}
				});


				ButtonTouch collaborativeButton = new ButtonTouch("collaborative");
				collaborativeButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						star1.getControler().setCollaborative(!star1.getControler().isCollaborative());
						star2.getControler().setCollaborative(!star2.getControler().isCollaborative());
					}
				});

				ButtonTouch isLinkedButton = new ButtonTouch("link branches");
				isLinkedButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						star1.getControler().setLinkBranches(!star1.getControler().isLinkBranches());
						star2.getControler().setLinkBranches(!star2.getControler().isLinkBranches());
					}
				});

				ButtonTouch isPastTraceButton = new ButtonTouch("past trace");
				isPastTraceButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						star1.getControler().setPastTrace(!star1.getControler().isPastTrace());
						star2.getControler().setPastTrace(!star2.getControler().isPastTrace());
					}
				});
				
				ButtonTouch compareButton = new ButtonTouch("compare");
				compareButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						StarComparator comparator = new StarComparator(
								star1.getControler().getInternCircleRadius(), 
								star1.getControler().getExternCircleRadius(),
								star1,
								star2);
						comparator.setName("Star comparator");
						
						comparator.getControler().init();
						comparator.setBranchThickness(3);
						comparator.getControler().setLinkBranches(true);
						comparator.setLinkThickness(3);
						comparator.setCentralPolygonColor(Color.BLACK);
						comparator.setCentralPolygonThickness(1);
						comparator.getControler().setCollaborative(false);
						comparator.getControler().setPastTrace(false);
						comparator.getControler().setPastTraceTime(5000);
						comparator.getControler().setPastTraceFrequency(50);
						comparator.setVisible(true);
						
						
						comparator.setBounds(0,(int) star1.getBounds().getMaxY(), (int)star1.getBounds().getWidth(), (int) star1.getBounds().getHeight());
						comparator.setBorder(BorderFactory.createTitledBorder("star comparator"));
						frame.getContentPane().add(comparator);
						frame.getContentPane().validate();
						comparator.initStar();
						comparator.getControler().createAllBranches();
						comparator.getControler().orderBranchesByAngle();
						

					}
				});


				frame.pack();
				frame.setVisible(true);

				frame.getContentPane().setLayout(null);

				JPanel rightPane = new JPanel();
				rightPane.setLayout(new BoxLayout(rightPane, BoxLayout.Y_AXIS));
				rightPane.add(exitButton);
				rightPane.add(collaborativeButton);
				rightPane.add(isLinkedButton);
				rightPane.add(isPastTraceButton);
				rightPane.add(compareButton);
				rightPane.setBounds((int)star2.getBounds().getMaxX(), 0, (int) (frame.getBounds().getWidth()-star2.getBounds().getMaxX()), (int)frame.getBounds().getHeight());

				frame.getContentPane().add(star1);
				frame.getContentPane().add(star2);
				frame.getContentPane().add(rightPane);
				frame.pack();

				frame.setVisible(true);


				buildBranchList1(star1);
				buildBranchList2(star2);

				try {
					Runtime.getRuntime().exec("cmd.exe /c start Touch2Tuio\\x64\\Release\\Touch2Tuio_x64.exe StarComparatorTest");
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}


	private static void buildBranchList1(StarComponent star){
		star.initStar();

		StarBranchSlider visibility = new StarBranchSlider(
				star, 
				E_PossibleBranches.VISIBILITY, 
				Color.BLUE,
				new Ladder(E_PossibleBranches.VISIBILITY.getMaxValue(),
						true,
						true, 
						Color.BLUE, 
						Color.BLACK),
						Math.toRadians(0),
						true);
		visibility.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.GREEN, new Color(150,255,150)});
		visibility.setValue(5);

		StarBranchSlider dutchAngle = new StarBranchSlider(
				star, 
				E_PossibleBranches.DUTCH_ANGLE, 
				Color.GREEN,
				new Ladder(E_PossibleBranches.DUTCH_ANGLE,
						true, 
						true, 
						Color.GREEN, 
						Color.GREEN),
						Math.toRadians(40),
						true);
		dutchAngle.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.BLUE, new Color(100,100,255)});
		dutchAngle.setValue(135);

		StarBranchSlider minShotDuration = new StarBranchSlider(
				star, 
				E_PossibleBranches.MIN_SHOT_DURATION, 
				Color.YELLOW,
				new Ladder(E_PossibleBranches.MIN_SHOT_DURATION,
						true, 
						true, 
						Color.RED, 
						Color.BLUE),
						Math.toRadians(120),
						true);
		minShotDuration.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});

		StarBranchSlider shotType = new StarBranchSlider(
				star, 
				E_PossibleBranches.SHOT_TYPE, 
				Color.ORANGE,
				new Ladder(E_PossibleBranches.SHOT_TYPE,
						true, 
						true, 
						Color.BLUE, 
						Color.BLACK),
						Math.toRadians(200),
						true);
		shotType.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});
		shotType.setValue(2);

		StarBranchSlider intensity = new StarBranchSlider(
				star, 
				E_PossibleBranches.INTENSITY, 
				Color.CYAN,
				new Ladder(E_PossibleBranches.INTENSITY,
						true, 
						true, 
						Color.BLUE,
						Color.BLACK),
						Math.toRadians(290),
						true);
		intensity.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});
		intensity.setValue(3);

	

		star.getControler().addBranch(minShotDuration);
		star.getControler().addBranch(visibility);
		star.getControler().addBranch(dutchAngle);
		star.getControler().addBranch(shotType);
		star.getControler().addBranch(intensity);

		star.getControler().orderBranchesByAngle();

		star.getControler().createAllBranches();
		
		star.getControler().createDependencies();
		
		star.revalidate();
	}
	
	
	private static void buildBranchList2(StarComponent star){
		star.initStar();
		
		StarBranchSlider visibility = new StarBranchSlider(
				star, 
				E_PossibleBranches.VISIBILITY, 
				Color.BLUE,
				new Ladder(E_PossibleBranches.VISIBILITY.getMaxValue(),
						true,
						true, 
						Color.BLUE, 
						Color.BLACK),
						Math.toRadians(0),
						true);
		visibility.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.GREEN, new Color(150,255,150)});
		visibility.setValue(60);
		
		StarBranchSlider dutchAngle = new StarBranchSlider(
				star, 
				E_PossibleBranches.DUTCH_ANGLE, 
				Color.GREEN,
				new Ladder(E_PossibleBranches.DUTCH_ANGLE,
						true, 
						true, 
						Color.GREEN, 
						Color.GREEN),
						Math.toRadians(40),
						true);
		dutchAngle.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.BLUE, new Color(100,100,255)});
		dutchAngle.setValue(30);
		
		StarBranchSlider minShotDuration = new StarBranchSlider(
				star, 
				E_PossibleBranches.MIN_SHOT_DURATION, 
				Color.YELLOW,
				new Ladder(E_PossibleBranches.MIN_SHOT_DURATION,
						true, 
						true, 
						Color.RED, 
						Color.BLUE),
						Math.toRadians(120),
						true);
		minShotDuration.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});
		minShotDuration.setValue(75.0);

		StarBranchSlider shotType = new StarBranchSlider(
				star, 
				E_PossibleBranches.SHOT_TYPE, 
				Color.ORANGE,
				new Ladder(E_PossibleBranches.SHOT_TYPE,
						true, 
						true, 
						Color.BLUE, 
						Color.BLACK),
						Math.toRadians(200),
						true);
		shotType.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});
		shotType.setValue(2);

		StarBranchSlider intensity = new StarBranchSlider(
				star, 
				E_PossibleBranches.INTENSITY, 
				Color.CYAN,
				new Ladder(E_PossibleBranches.INTENSITY,
						true, 
						true, 
						Color.BLUE,
						Color.BLACK),
						Math.toRadians(290),
						true);
		intensity.setThumbColors(new Color[]{Color.BLACK, Color.GRAY}, new Color[]{Color.RED, new Color(255,200,200)});
		intensity.setValue(7);
		

		star.getControler().addBranch(minShotDuration);
		star.getControler().addBranch(visibility);
		star.getControler().addBranch(dutchAngle);
		star.getControler().addBranch(shotType);
		star.getControler().addBranch(intensity);
		

		star.getControler().orderBranchesByAngle();


		star.getControler().createAllBranches();
		
		star.getControler().createDependencies();
		
		star.revalidate();
	}
}
